package com.hsc.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsc.commonutils.JwtUtils;
import com.hsc.commonutils.MD5;
import com.hsc.educenter.entity.UcenterMember;
import com.hsc.educenter.entity.vo.RegisterVo;
import com.hsc.educenter.mapper.UcenterMemberMapper;
import com.hsc.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsc.servicebase.exceptionHandle.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-17
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        //手机号和密码进行非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"手机号或者密码不能为空");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember member1 = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(member1==null){
            throw new GuliException(20001,"手机号为空，登录失败");
        }
        //判断密码是否正确
        //密码进行md5加密后再与数据库密码进行比较
        String encrypt = MD5.encrypt(password);
        if(!encrypt.equals(member1.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        //判断用户是否被禁用
        if (member1.getIsDisabled()) {
            throw new GuliException(20001,"用户权限被禁用");
        }
        //登录成功
        //生成token字符串，使用jwt工具类
        String token = JwtUtils.getJwtToken(member1.getId(), member1.getNickname());
        return token;
    }

    //注册得方法
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册得数据
        String code = registerVo.getCode();//验证码判断
        String mobile = registerVo.getMobile();//手机号判断
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码
        //手机号和密码非空判断
        if(StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile)){
            throw new GuliException(20001,"注册失败");
        }
        //判断验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码校验失败");
        }
        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer result = baseMapper.selectCount(wrapper);
        if (result>0){
            throw new GuliException(20001,"手机号已经注册");
        }
        //添加数据到数据库重
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");//设置一个默认头像
        int i = baseMapper.insert(member);
        if(i!=1){
            throw new GuliException(20001,"注册失败");
        }
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {
        Integer count = baseMapper.countRegisterDay(day);

        return count;
    }
}
